package com.pro.xwt.rest.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.pro.xwt.common.mapper.BDictionaryMapper;
import com.pro.xwt.common.mapper.BEmployeeMapper;
import com.pro.xwt.common.model.BDictionary;
import com.pro.xwt.common.model.BEmployee;
import com.pro.xwt.common.vo.TreeNode;
import com.pro.xwt.rest.service.TreeNodeService;
@Service("treeNodeService")
public class TreeNodeServiceImpl implements TreeNodeService{

	@Autowired
	BDictionaryMapper dictMapper;
	@Autowired
	BEmployeeMapper empMapper;
	
	public List<TreeNode<Integer,String>> selectDeptEmpTree(BEmployee be) {
		
		List<BDictionary> deptList = dictMapper.findByDictType("DEPARTMENT");
		
		Map<Integer,String> deptMap = new HashMap<Integer,String>();
		for (int i = 0; i < deptList.size(); i++) {
			deptMap.put(deptList.get(i).getId(), deptList.get(i).getName());
		}	
		
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("deleteState", '0');
		PageHelper.orderBy("department_id desc");
		List<BEmployee> empList = empMapper.queryListAll(map);
		for (int i = 0; i < empList.size(); i++) {
			if (empList.get(i).getId().equals(be.getId())) {
				empList.remove(i);
			}
		}
		List<TreeNode<Integer,String>> objectList = new ArrayList<TreeNode<Integer,String>>();
		
		for(Integer dataKey : deptMap.keySet()){
			List<TreeNode<String, Integer>> childrenList = new ArrayList<TreeNode<String, Integer>> ();
			TreeNode<Integer,String> object = new TreeNode<Integer,String>();
			//System.out.println("键为："+dataKey+"--值为："+deptMap.get(dataKey));
			object.setId(dataKey);
			object.setLabel(deptMap.get(dataKey));
			object.setType(false);
			for (int i = 0; i < empList.size(); i++) {
				if (empList.get(i).getDepartmentId() == dataKey) {
					TreeNode<String, Integer> children = new TreeNode<String, Integer>();
					children.setId(empList.get(i).getId());
					children.setLabel(empList.get(i).getName());
					children.setType(true);
					childrenList.add(children);
				}
			}
			object.setChildren(childrenList);
			//childrenList.clear();
			objectList.add(object);
		}
		
		return objectList;
	}
	
}
